/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cassys.dataaccess.hibernate;

import cassys.dataaccess.DAOException;
import cassys.dataaccess.UserDAO;
import cassys.datatransferobjects.LecturerDTO;
import cassys.datatransferobjects.StudentDTO;
import cassys.datatransferobjects.UserDTO;
import cassys.exceptions.HibernateAccessLayerException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Priyanka
 */
public class UserDAOImpl extends AbstractDao implements UserDAO {

    @Override
    public void insertOrUpdateUser(UserDTO usr) {
        try {
            Logger.getLogger(getClass().getName()).log(Level.INFO, "Updating User" + usr.getUserId());
            /*
            if (usr.getRole().equals("lecturer")) {
                LecturerDTO lecturer = new LecturerDTO();
                lecturer.setPassword(usr.getPassword());
                lecturer.setEmail(usr.getEmail());
                lecturer.setRole(usr.getRole());
                lecturer.setUserId(usr.getUserId());
                super.saveOrUpdate(lecturer);
            } else if (usr.getRole().equals("student")) {
                StudentDTO student = new StudentDTO();
                student.setPassword(usr.getPassword());
                student.setEmail(usr.getEmail());
                student.setRole(usr.getRole());
                student.setUserId(usr.getUserId());
                super.saveOrUpdate(student);
            }
             * */
            
            super.saveOrUpdate(usr);

        } catch (Throwable t) {
            throw new HibernateAccessLayerException(t.toString());

        }
    }

    @Override
    public void deleteUser(UserDTO usr) {
        try {
            Logger.getLogger(getClass().getName()).log(Level.INFO, "Deleting User" + usr.getUsername());

            super.delete(usr);
            /*
            if (usr.getRole().equals("lecturer")) {
                LecturerDTO lecturer = (LecturerDTO) super.find(LecturerDTO.class, usr.getUsername());

                super.delete(lecturer);
            } else if (usr.getRole().equals("student")) {
                StudentDTO student = (StudentDTO) super.find(StudentDTO.class, usr.getUsername());

                super.delete(student);
            }
            super.delete(usr);
             * */
            
        } catch (Throwable t) {
            throw new HibernateAccessLayerException(t.toString());
        }
    }

    @Override
    public UserDTO findUser(String userId) {
        UserDTO p = null;
        try {
            Logger.getLogger(getClass().getName()).log(Level.INFO, "Finding User " + userId);
            p = (UserDTO) super.find(UserDTO.class, userId);
        } catch (Throwable t) {
            throw new HibernateAccessLayerException(t.toString());

        }
        return p;
    }

    @Override
    public ArrayList<UserDTO> findAllUser() {
        try {
            return (ArrayList<UserDTO>) super.findAll(UserDTO.class);
        } catch (Throwable t) {
            throw new HibernateAccessLayerException(t.toString());
        }
    }

    @Override
    public UserDTO findUserByName(String username) throws DAOException {
         UserDTO p = null;
        try {
            Logger.getLogger(getClass().getName()).log(Level.INFO, "Finding User " + username);
            p = (UserDTO) super.findByParam(UserDTO.class, "username", username);
        } catch (Throwable t) {
            throw new HibernateAccessLayerException(t.toString());

        }
        return p;
    }
}
